package com.example.webview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{
    private static final String TAG = "MainActivity";

    private WebView webView;

    //fastJson
    //Gson
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //前进
        Button btn_go = (Button) findViewById(R.id.btn_go);
        btn_go.setOnClickListener(this);

        //刷新
        Button btn_reload = (Button) findViewById(R.id.btn_reload);
        btn_reload.setOnClickListener(this);

        //后退
        Button btn_back = (Button) findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);

        webView = (WebView) findViewById(R.id.wv_web);
        webView.loadUrl("http://www.baidu.com");
        // 设置支持 javaScript
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);


        //设置浏览器客户端
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //获取加载进度
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
//                super.onProgressChanged(view, newProgress);

                Log.i(TAG, "onProgressChanged: "+newProgress);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_go:
                //前进
                webView.goForward();
                break;
            case R.id.btn_reload:
                //刷新
                webView.reload();
                break;

            case R.id.btn_back:
                //后退
                webView.goBack();
                break;
        }

    }
}
